package com.tnmi.base;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tnmi.exception.code.ExceptionCode;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @Classname R
 * @Description TODO
 * @Date 2023/7/2 21:47
 * @Created by tnmi
 **/
@Data
public class R implements Serializable {
    private Integer code;

    private String  msg;

    private Map<String,Object> extra = new HashMap<>();

    private R(){}

    public static R ok(){
        R results = new R();
        results.setCode(ExceptionCode.SUCCESS.getCode());
        results.setMsg(ExceptionCode.SUCCESS.getMsg());
        return results;
    }

    public static R error(){
        R r = new R();
        r.setCode(ExceptionCode.FATL.getCode());
        r.setMsg(ExceptionCode.FATL.getMsg());
        return r;
    }

    public R code(Integer code){
        this.setCode(code);
        return this;
    }
    public R msg(String  msg){
        this.setMsg(msg);
        return this;
    }
    public R data(String key,Object val){
        Map map = new HashMap();
        map.put(key,val);
        this.setExtra(map);
        return this;
    }
    public R data(Map<String,Object> map){
        this.setExtra(map);
        return this;
    }
    public R data(Page page){
        Object rows = page.getRecords();
        Map<String,Object> map = new HashMap<>();
        map.put("list",rows);
        this.setExtra(map);
        return this;
    }
}
